//
//  MXLoginRegistVC.swift
//  XMPPTest
//
//  Created by 陈学明 on 17/2/9.
//  Copyright © 2017年 muxue. All rights reserved.
//

import UIKit

class MXLoginRegistVC: MXBaesVC,LoginRegistDelagate {
    var userName:String?
    var password:String?
    var appDelegate:AppDelegate!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        appDelegate = UIApplication.shared.delegate as! AppDelegate
        appDelegate.loginRegistDelegate = self
        
        let userNamefield =  MXTextField.init(title: "用户名", placeholder: "请输入用户名")
        userNamefield.backgroundColor = UIColor.gray
        let passWordfield = MXTextField.init(title: "密码", placeholder: "请输入密码")
        passWordfield.backgroundColor = UIColor.gray
        
        view.sd_addSubviews([userNamefield,passWordfield])
        
        userNamefield.sd_layout()
            .centerXEqualToView(view)?
            .topSpaceToView(view,200)?
            .heightIs(50)?.widthIs(300)
        
        passWordfield.sd_layout()
            .centerXEqualToView(view)?
            .topSpaceToView(userNamefield,40)?
            .heightIs(50)?.widthIs(300)
        
        userNamefield.textField?.rac_textSignal().subscribeNext({ (x) in
            //print(x ?? "")
            self.userName = x as String?
        })
        
        passWordfield.textField?.rac_textSignal().subscribeNext({ (x) in
//            print(x ?? "")
            self.password = x as String?
        })
        
        LXFListeningKeyboard.isShow().view = self.view
        LXFListeningKeyboard.isShow().responders = [userNamefield.textField!,passWordfield.textField!]
        
        let loginBtn = UIButton(type: .custom)
        loginBtn.backgroundColor = UIColor.yellow
        loginBtn.setTitleColor(UIColor.orange, for: UIControlState(rawValue:0))
        loginBtn.setTitleColor(UIColor.black, for: .highlighted)
        loginBtn.setTitle("登录", for: UIControlState(rawValue:0))
        loginBtn.rac_signal(for: .touchUpInside).subscribeNext { (x) in
            print("登录")
            self.loginOrRegist(isRegist: false)
        }
        
        let regist = UIButton(type: .custom)
        regist.backgroundColor = UIColor.yellow
        regist.setTitleColor(UIColor.orange, for: UIControlState(rawValue:0))
        regist.setTitleColor(UIColor.black, for: .highlighted)
        regist.setTitle("注册", for: UIControlState(rawValue:0))
        regist.rac_signal(for: .touchUpInside).subscribeNext { (x) in
            print("注册")
            self.loginOrRegist(isRegist: true)
        }
        view.sd_addSubviews([loginBtn,regist])
        
        loginBtn.sd_layout()
            .leftEqualToView(passWordfield)?
            .topSpaceToView(passWordfield,20)?
            .heightIs(30)?.widthIs(80)
        
        regist.sd_layout()
            .rightEqualToView(passWordfield)?
            .topSpaceToView(passWordfield,20)?
            .heightIs(30)?.widthIs(80)
        
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
   fileprivate func loginOrRegist(isRegist:Bool){
    guard let name = userName,
        let pass = password else {
            print("请输入用户名、密码‘服务器地址")
            return;
        }
    
        if name.utf8.count>0 && pass.utf8.count>0 {
            print(name + pass)
            
            let defaults = UserDefaults.standard
            defaults.set(name, forKey: "USERNAME")
            defaults.set(pass, forKey: "PASSWORD")
            defaults.set("192.168.61.25", forKey: "SERVER_KEY")
           //保存
            defaults.synchronize()
            appDelegate.connect(isRegist: isRegist)
//            loginSuccess()
        }
    
    }
    func loginSuccess() {
        appDelegate.window?.rootViewController = MXBarVC();
    }
    
    func showAlert(title: String) {
        let alert = UIAlertController(title: "提示", message: title, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .cancel, handler: { (x) in
            print(x)
        }))
        self.present(alert, animated: true, completion: nil)
    }
}
